<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 登录头像开始 -->
      <div class="login-logo">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录头像结束 -->

      <!-- 登录信息开始 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        class="logo-from"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
            placeholder="请输入用户名"
          >
          </el-input>
        </el-form-item>
        <!-- 用户密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
            placeholder="请输入密码"
          >
          </el-input>
        </el-form-item>
        <!-- 登录和重置按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 登录信息结束 -->
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      // 用户信息
      loginForm: {
        username: '',
        password: '',
      },
      // 验证表单信息
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 12,
            message: '长度在 6 到 12 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    // 点击登录按钮，进行登录验证
    login() {
      // console.log(this)
      this.$refs.loginFormRef.validate(async (valid) => {
        // console.log(valid)
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        // console.log(res)
        // console.log(res.data.token)
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    },
    // 点击重置按钮，重置表单数据
    reset() {
      this.$refs.loginFormRef.resetFields()
    },
  },
}
</script>

<style lang="less" scoped>
.login-container {
  position: relative;
  height: 100%;
  background-color: #2b4b6b;

  .login-box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    height: 350px;
    width: 500px;
    background-color: #fff;

    // 登录头像开始
    .login-logo {
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      padding: 10px;
      width: 130px;
      height: 130px;
      background-color: #fff;
      border: 1px solid #eee;
      border-radius: 50%;
      box-shadow: 0 0 10px #ddd;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #fff;
      }
    }
    // 登录头像结束

    // 登录信息开始
    .logo-from {
      position: absolute;
      top: 35%;
      padding: 0 15px;
      width: 100%;
      box-sizing: border-box;

      .btns {
        display: flex;
        justify-content: right;
      }
    }

    // 登录信息结束
  }
}
</style>